package org.ne.banksys.controller;

import org.apache.ibatis.annotations.Delete;
import org.ne.banksys.common.Result;
import org.ne.banksys.entity.Card;
import org.ne.banksys.entity.Trade;
import org.ne.banksys.service.CardService;
import org.ne.banksys.service.TradeService;
import org.ne.banksys.service.UserService;
import org.ne.banksys.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wey
 * @date 2025/6/18 下午2:32 06
 **/

@RestController
@RequestMapping("/trade")
public class TradeController {

    @Autowired
    private TradeService tradeService;
    @Autowired
    private UserService userService;
    @Autowired
    private CardService cardService;




    @GetMapping("/myTradeRecord")
    public Result myTradeRecord() {
        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));


        List<Trade> trades = tradeService.getAll();
        List<Card> myCards = cardService.selectCardByCustomerId(id);
        List<Trade> myTrades = new ArrayList<>();
        for (Card card : myCards) {

            for (Trade trade : trades) {
                if (trade.getCardId().equals(card.getCardId())) {
                    myTrades.add(trade);
                }
            }

        }

        return Result.success(myTrades);

    }


    @PostMapping("/add")
    public Result add(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        String tradeType = (String) para.get("tradeType");
        BigDecimal amount = new BigDecimal ((String) para.get("amount"));
        String remark = (String) para.get("remark");

        Trade trade = new Trade();
        trade.setTradeDate(new Timestamp(System.currentTimeMillis()));
        trade.setTradeType(tradeType);
        trade.setCardId(cardId);
        trade.setTradeMoney(amount);
        trade.setRemark(remark);
        tradeService.add(trade);
        return Result.success();
    }

    @Delete("/delete")
    public Result delete(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        Long tradeId = Long.parseLong((String) para.get("tradeId"));

        Trade trade = tradeService.getById(tradeId);
        if (!trade.getCardId().equals(cardId)) {
            return Result.error("wrong card id");
        }
        tradeService.delete(tradeId);
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Map<String, Object> para) {
        Long tradeId = Long.parseLong ((String) para.get("tradeId"));
        String remark = (String) para.get("remark");

        Trade trade = tradeService.getById(tradeId);
        Card card = cardService.selectCardById(trade.getCardId());
        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong(info.get("customerId").toString());
        if (! id.equals(card.getCustomerId())) {
            return Result.error("wrong id");
        }
        trade.setRemark(remark);
        tradeService.update(trade);
        return Result.success(tradeService.getById(tradeId));
    }
}

